let now = new Date();                    //当前日期     
let nowDayOfWeek = now.getDay();         //今天本周的第几天     
let nowDay = now.getDate();              //当前日     
let nowMonth = now.getMonth();           //当前月     
let nowYear = now.getYear();             //当前年     
nowYear += (nowYear < 2000) ? 1900 : 0;  // 

//获得某月的天数
export function getMonthDays(myMonth){     
  let monthStartDate = new Date(nowYear, myMonth, 1);      
  let monthEndDate = new Date(nowYear, myMonth + 1, 1);
  let days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);      
  return days;      
}

/**
 * 当前时间格式化
 * date: new Date()
 */
export function formatDate(date) {
  if (date == null || date == "") return "";
  // let date = new Date(cellValue)
  let year = date.getFullYear()
  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return year + '-' + month + '-' + day
  // return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
/**
 * 昨日时间格式化
 * d: 0今天, 1昨天...
 */
export function getYesterDayDate(d = 0) {
  let yesterDayDate = new Date(nowYear, nowMonth, nowDay - d);      
  return formatDate(yesterDayDate);
}

/**
 * 获得本/上周的开始日期 
 * week：0本周，7上周
 */
export function getWeekStartDate(week = 0) {      
  let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - week);      
  return formatDate(weekStartDate);     
}
    
//获得本/上周的结束日期     
export function getWeekEndDate(week = 0) {      
  let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - week));      
  return formatDate(weekEndDate);     
}

/**
 * 获得本/上月的开始日期
 * month: 0本月，1上月...
 */
export function getMonthStartDate(month = 0){     
  let monthStartDate = new Date(nowYear, nowMonth - month, 1);      
  return formatDate(monthStartDate);     
}

//获得本/上月的结束日期     
export function getMonthEndDate(month = 0){
  let monthEndDate = new Date(nowYear, nowMonth - month, getMonthDays(nowMonth - month));      
  return formatDate(monthEndDate);
}
